
<#
.SYNOPSIS 
Hydration Script for MDTEx

.DESCRIPTION
_TEMPLATE_

.NOTES
Copyright Keith Garner (KeithGa@DeploymentLive.com), All rights reserved.

.LINK
http://MDTEx.CodePlex.com

#>

[CmdletBinding()]
param(
    [string] $DownloadPath,
    [string] $HostCache,
    [parameter(mandatory=$true,HelpMessage="Location of Local Deployment Share.")]
    [string] $DeploymentLocalPath, # Example: c:\DeploymentShare

    [parameter(ValueFromRemainingArguments=$true)] $Remaining
)

if ( -not ( Test-Path "$env:programfiles\7-Zip\7z.exe" ))
{
    throw "missing 7-zip"
}

if ( -not $DownloadPath ) { $DownloadPath = join-path $PSScriptRoot "..\..\Downloads" }


Write-LogVerbose "Install bits for Applications"

foreach ( $Folder in "IEx86","IEx64","KMDFx86","KMDFx64","DotNet45" )
{
    copy-itemwithprogress /e """$DownloadPath\$Folder""" """$DeploymentLocalPath\Applications\$Folder"""
}

if ( $HostCache )
{
    "Create Shortcuts (Internal regression testing)" | write-Logverbose

    copy-itemwithprogress /e """$HostCache\Deploy""" """$DeploymentLocalPath\Operating Systems\Deploy""" /xf install.wim
    foreach ( $Folder in "Create","Upgrade")
    {
        if (( -not ( Test-Path "$DeploymentLocalPath\Operating Systems\$Folder")) -and (Test-Path "$HostCache\$Folder") )
        {
            cmd.exe /c mklink /J "$DeploymentLocalPath\Operating Systems\$Folder" "$HostCache\$Folder"
        }
    }
}

[xml]$OSList = Get-Content "$DeploymentLocalPath\Control\operatingSystems.xml"
foreach ( $OSSrc in $oslist.oss.os.Source )
{
    "Find $OSSrc " | Write-verbose
    if ( -not ( Test-Path "$DeploymentLocalPath\$OSSrc"))
    {
        $OSID = Split-Path -Leaf $OSSrc

        if ( Test-Path "$PSSCriptRoot\..\..\Downloads\$OSID\*.iso")
        {
            $ISOPath = Get-ChildItem "$PSSCriptRoot\..\..\Downloads\$OSID\*.iso" | Select-Object -First 1 -ExpandProperty FullName

            if ( $OSSrc -match "Deploy" )
            {
                "extract [$ISOPath] without install.wim to $DeploymentLocalPath\$OSSrc" | write-verbose
                & "$env:programfiles\7-zip\7z.exe" x "-xr!install.wim" "-o$DeploymentLocalPath\$OSSrc" "$ISOPath" | Write-Verbose
            }
            elseif (( $OsSrc -match "Create") -or ( $OSSrc -match "Upgrade" ))
            {
                "extract [$ISOPath] with install.wim to $DeploymentLocalPath\$OSSrc" | write-verbose
                & "$env:programfiles\7-zip\7z.exe" x "-o$DeploymentLocalPath\$OSSrc" "$ISOPath" | Write-Verbose
            }
            else
            {
                "unknown OS source Type [$OSSrc] $OSID" | Write-Error
            }
        }
        else
        {
            "OSID not FOund: $OSID" | Write-error
        }

    }
}
